package srx.open.com.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

import srx.open.com.R;
import srx.open.com.mode.ClassItem;

/**
 *  附近菜单-分类adpater.
 */
public class ClassRecyclerAdapter extends RecyclerView.Adapter<ClassRecyclerAdapter.ClassViewHolder> {
    public int mSelectedItem = 0;
    private List<ClassItem> mClassItems = new ArrayList<ClassItem>();

    private OnItremCheckListener mOnItremCheckListener;

    public ClassRecyclerAdapter() {
    }

    public ClassRecyclerAdapter(List<ClassItem> classItems) {
        this.mClassItems = classItems;
    }

    public void setClassItems(List<ClassItem> classItems) {
        this.mClassItems = classItems;
        notifyDataSetChanged();
    }

    @Override
    public ClassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_nearby_select, parent, false);
        ClassViewHolder holder = new ClassViewHolder(v);
        return holder;
    }

    public void setOnItemCheckListener(OnItremCheckListener onItremCheckListener) {
        this.mOnItremCheckListener = onItremCheckListener;
    }

    @Override
    public void onBindViewHolder(ClassViewHolder topViewHolder, int pos) {
        ClassItem classItem = mClassItems.get(pos);
        String title = "全部";
        if (classItem != null) {
            title = classItem.getDescription();
        }
        topViewHolder.name_rbtn.setText(title);
        topViewHolder.name_rbtn.setChecked(pos == mSelectedItem);
    }

    @Override
    public int getItemCount() {
        return mClassItems.size();
    }

    public class ClassViewHolder extends RecyclerView.ViewHolder {
        public RadioButton name_rbtn;
        public ClassViewHolder(final View itemView) {
            super(itemView);
            name_rbtn = (RadioButton) itemView.findViewById(R.id.name_rbtn);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    notifyDataSetChanged();
                    // item单击事件处理.
                    if (mOnItremCheckListener != null)
                        mOnItremCheckListener.onItemCheck(v, mSelectedItem);
                }
            };
            itemView.setOnClickListener(clickListener);
            name_rbtn.setOnClickListener(clickListener);
        }
    }

    public static class OnItremCheckListener {
            public void onItemCheck(View v, int pos) {
            }
    }

}
